<?php
namespace App\Services;

use App\Models\ArticleCate;

class ArticleCateService extends CommonService{
	
	
	/**
	 * 前台分页查询
	 */
	function paginateCate($where = null,$page = 1,$pagesize =10){
		$skip = ($page - 1) * $pagesize;
		try{
			$result = ArticleCate::where($where)
		 				->skip($skip)
		 				->take($pagesize)
		 				->get();
		 	$count = ArticleCate::where($where)->count();		
		 	if(count($result) != 0){
            	return $this->show(1,'查询成功',['data'=>$result,'count'=>$count]);
            }else{
            	return $this->show(0,'查询失败',$result);
            }
		}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
	}
	
	
	/**
	 * 分页查询
	 * where([
    *			['status', '=', '1'],
    *			['subscribed', '<>', '1'],
    *	])
	 */
    function paginateInfo($where = null,$limit = 10){
    	try{
    		$result = ArticleCate::where($where)
    					->orderBy('sort', 'asc')
    					->latest()
            			->paginate($limit);
           
            if(count($result) != 0){
            	return $this->show(1,'查询成功',$result);
            }else{
            	return $this->show(0,'查询失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
    		
    }
    
    
   
    /**
     * 添加管理员
     */
    function addInfo($param = array()){
    	try{
    		$model = new ArticleCate();
    		foreach($param as $key => $value) {
                $model->$key = $value;
            }
          
            $result = $model->save();
            if($result){
            	return $this->show(1,'添加成功',$result);
            }else{
            	return $this->show(0,'添加失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
    }
    
    /**
     * 更新管理员
     */
    function updateInfo($where = null,$param=[]){
    	try{
    		$result = ArticleCate::where($where)->update($param);
    		
            if($result){
            	return $this->show(1,'更新成功',$result);
            }else{
            	return $this->show(0,'更新失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
    	
    	
    }
    
    
    /**
     * 查询单条管理员
     */
    function findInfo($where = null){
    	try{
    		$result = ArticleCate::where($where)->first();

            if($result){
            	return $this->show(1,'查询成功',$result);
            }else{
            	return $this->show(0,'查询失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
    }
    
    
    /**
     *查询所有用户
     */
    function findAllInfo($where = null){
    	try{
    		$result = ArticleCate::where($where)->orderBy('sort', 'asc')->latest()->get();
           
            if($result){
            	return $this->show(1,'查询成功',$result);
            }else{
            	return $this->show(0,'查询失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
    	return $result = BaseUser::get();
    }
    
    /**
     * 删除
     */
    function deleteInfo($where = null){
    	try{
    		$result = ArticleCate::where($where)->delete();
            if($result){
            	return $this->show(1,'删除成功',$result);
            }else{
            	return $this->show(0,'删除失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
    }
}
